package cn.ee.dfs.esb.controller;

import cn.ee.dfs.common.annotation.Log;
import cn.ee.dfs.common.controller.BaseController;
import cn.ee.dfs.common.entity.DfsConstant;
import cn.ee.dfs.common.entity.DfsResponse;
import cn.ee.dfs.common.entity.QueryRequest;
import cn.ee.dfs.common.exception.DfsException;
import cn.ee.dfs.common.utils.DfsUtil;
import cn.ee.dfs.esb.entity.ServiceInfo;
import cn.ee.dfs.esb.entity.UserService;
import cn.ee.dfs.esb.service.IServiceInfoService;
import cn.ee.dfs.esb.service.IUserServiceService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * 用户服务表 Controller
 *
 * @author TWJ
 * @date 2019-08-14 15:34:25
 */
@Slf4j
@Validated
@RestController
public class UserServiceController extends BaseController {

    @Autowired
    private IUserServiceService userServiceService;
    @Autowired
    private IServiceInfoService serviceInfoService;

    @GetMapping(DfsConstant.VIEW_PREFIX + "userService")
    private String userServiceIndex() {
        return DfsUtil.view("userService/userService");
    }

    @GetMapping("userService")
    @RequiresPermissions("userService:list")
    public DfsResponse getAllUserServices(UserService userService) {
        return new DfsResponse().success().data(userServiceService.findUserServices(userService));
    }

    @GetMapping("userService/list/provide/{userName}")
    @RequiresPermissions("userService:view")
    public DfsResponse userServiceListProvide(@PathVariable("userName") String userName, QueryRequest request, ServiceInfo serviceInfo) {
        serviceInfo.setPrincipal(userName);
        Map<String, Object> dataTable = getDataTable(this.serviceInfoService.findServiceInfos(request, serviceInfo));
        return new DfsResponse().success().data(dataTable);
    }

    @GetMapping("userService/list/apply/{userId}")
    @RequiresPermissions("userService:view")
    public DfsResponse userServiceListApply(@PathVariable("userId") String userId, QueryRequest request, UserService userService) {
        userService.setUserId(Long.parseLong(userId));
        Map<String, Object> dataTable = getDataTable(this.serviceInfoService.findUserServices(request, userService));
        return new DfsResponse().success().data(dataTable);
    }


    @GetMapping("userService/list/apply/all")
    @RequiresPermissions("userService:auditView")
    public DfsResponse userServiceAllListApply(QueryRequest request, UserService userService) {
        Map<String, Object> dataTable = getDataTable(this.serviceInfoService.findUserServices(request, userService));
        return new DfsResponse().success().data(dataTable);
    }


    @Log("新增申请服务UserService")
    @PostMapping("userService")
    @RequiresPermissions("userService:add")  //
    public DfsResponse addUserService(@Valid UserService userService) throws DfsException {
        log.info("-- userService ->" + userService);
        try {
            UserService one = userServiceService.getOne(Wrappers.lambdaQuery(new UserService(userService.getUserId(), userService.getServiceId())).last("LIMIT 1"));
            if (one != null) throw new DfsException("该服务已申请");
            this.userServiceService.createUserService(userService);
            return new DfsResponse().success();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new DfsException(e.getMessage());
        }
    }

    @Log("删除UserService")
    @GetMapping("userService/delete/{userServiceIds}")
    @RequiresPermissions("userService:delete")
    public DfsResponse deleteUserService(@NotBlank(message = "{required}") @PathVariable String userServiceIds) throws DfsException {
        try {
            String[] ids = userServiceIds.split(StringPool.COMMA);
            this.userServiceService.deleteUserService(ids);
//            this.serviceInfoService.deleteUsers(ids);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "删除UserService失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @Log("审核申请服务userService")
    @PostMapping("userService/update")
//    @RequiresPermissions("userService:update")
    public DfsResponse updateUserService(UserService userService) throws DfsException {
        try {
            this.userServiceService.updateUserServiceStatus(userService);
            return new DfsResponse().success();
        } catch (Exception e) {
            String message = "修改UserService失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }

    @PostMapping("userService/excel")
//    @RequiresPermissions("userService:export")
    public void export(QueryRequest queryRequest, UserService userService, HttpServletResponse response) throws DfsException {
        try {
            List<UserService> userServices = this.userServiceService.findUserServices(queryRequest, userService).getRecords();
            ExcelKit.$Export(UserService.class, response).downXlsx(userServices, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new DfsException(message);
        }
    }
}
